{% comment %}
#    This file is part of OpenPolitik.
#
#    OpenPolitik is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    OpenPolitik is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with OpenPolitik.  If not, see <http://www.gnu.org/licenses/>.
{% endcomment %}
{% extends "layout.html" %}

{% block content %}
<script type = "text/javascript">
{% ifequal liu game.admin %}
function boot(email){
	if (confirm("Are you sure you want to kick this user?")){
		$("user_" + email).innerHTML = "Booting...";
		new Ajax.Request('/boot', {
			parameters: {game: '{{ game.key }}', user: email},
			onSuccess: function(response){
				$("user_" + email).innerHTML = "Booted.";
				setTimeout("Effect.Fade('user_" + email + "', {duration: 0.5, afterFinish: function(){$('user_" +
					email + "').style.display = 'none';}})", 1000);
			}
		});
	}
}
function getOrders(){
	if (confirm("Are you sure you want to view the orders?"))
		new Ajax.Updater("all_orders", '/get_orders', { method: "get", parameters: { game: '{{ game.key }}' }})
}

function updateInfo(){
	var d = $("info_date").value;
	var phase = $("info_phase").value;
	var due_date = $("info_due_date").value;

	$("info_msg").innerHTML = "Saving...";
	new Ajax.Request("/update_info", {
		parameters: { game: '{{ game.key }}', date: d, phase: phase, due_date: due_date},
		onSuccess: function(response){
			$("info_msg").innerHTML = "Saved.";
			$("current_date").innerHTML = d;
			$("current_phase").innerHTML = $("info_phase").options[$("info_phase").selectedIndex].text;
			$("current_due_date").innerHTML = due_date;
			setTimeout("$('info_msg').innerHTML = ''", 3000);
		}
	});
}
{% endifequal %}

{% ifnotequal game.state 0 %}
function submitOrders(){
	var orders = $("orders_ta").value;

	$("orders_msg").innerHTML = "Sending...";

	new Ajax.Request("/orders", {
		parameters: { game: '{{ game.key }}', orders: orders },
		onSuccess: function(response){
			$("orders").innerHTML = response.responseText;
			$("orders_msg").innerHTML = "Sent.";
			$("orders_ta").value = "";
			setTimeout("$('orders_msg').innerHTML = ''", 3000);
		}
	});
}
{% endifnotequal %}

{% if messageable %}
function deleteMessage(message){
	if (confirm("Delete this message?")){
		$("message_box").innerHTML = "Deleting...";
		new Ajax.Request("/delete_message", {
			parameters: { message: message },
			onSuccess: function(response){
				$("message_box").innerHTML = "Deleted.";
				setTimeout("$('message_box').innerHTML = ''", 3000);
				Effect.BlindUp("message_" + message, { duration: 0.3 });
			}
		});
	}
}
{% endif %}
</script>
<h2>{{ game.name }}</h2>
<div id = "users_list">
	{% ifnotequal game.state 0 %}
		<a href = "/view_map?game={{ game.key }}"><img src = "/images/smallmap.jpg" alt = "View Map" /></a><br />
	{% endifnotequal %}
	{% if messageable %}
		Send a message:
	{% else %}
		Users in game:
	{% endif %}
	<ul>
	{% for uig in uigs %}
		{% ifnotequal liu uig.user %}
			<li id = "user_{{ uig.user.email }}">
				{% if messageable %}
					<a href = "/compose/{{ uig.user.email|urlencode }}?game={{ game.key }}">{{ uig.user.nickname }}{% ifnotequal game.state 0 %} ( {{ uig.nation }} ){% endifnotequal %}</a>
					{% ifequal liu game.admin %}
						<a href = "#" onclick = "boot('{{ uig.user.email }}'); return false">Boot</a>
					{% endifequal %}
				{% else %}
					{{ uig.user.nickname }}
				{% endif %}
			</li>
		{% endifnotequal %}
	{% endfor %}
	</ul>
</div>
{% ifnotequal game.state 0 %}
	It is currently <span id = "current_date">{{ game.date }}</span>. You are {{ uig.nation }}.<br />
	The phase is <span id = "current_phase">{{ current_phase }}</span>.<br />
	Order due date: <span id = "current_due_date">{{ game.due_date }}</span><br />
	<br />
	Orders:<br />
	<div id = "orders">
		{% if orders %}
			{{ orders }}
		{% else %}
			<i>You have not yet submitted orders.</i><br />
		{% endif %}
	</div>
	<textarea id = "orders_ta" rows = "6" cols = "50"></textarea><br />
	<input type = "button" value = "Submit" onclick = "submitOrders()" />
	<div id = "orders_msg"></div>
{% else %}
	The game has not yet started.<br /><br />
{% endifnotequal %}

{% if messageable %}
	Messages:<br />
	{% if messages|length_is:0 %}
		<i>No messages...</i><br />
	{% else %}
		{% for m in messages %}
			<div class = "{% cycle light,dark %}" id = "message_{{ m.key }}">
				<div class = "left"><a href = "/message/{{ m.key }}">{{ m.subject }}</a></div>
				<div class = "right"><a href = "#" onclick = "deleteMessage('{{ m.key }}')" class = "delete">X</a></div>
				<div class = "clear"></div>
			</div>
		{% endfor %}
	{% endif %}
{% endif %}
<div id = "message_box"></div>
<br />
{% ifequal liu game.admin %}
	{% ifequal game.state 0 %}
		<a href = "/start_game?game={{ game.key }}">Start Game</a><br />
		<a href = "{{ join_url }}" onclick = "return confirm('Are you sure you want to cancel the game?')">Cancel Game</a><br />
	{% else %}
		<a href = "{{ join_url }}" onclick = "return confirm('Are you sure you want to end the game?')">End Game</a><br />
		<a href = "#" onclick = "getOrders(); return false">See Orders</a><br />
		<div id = "all_orders"></div>
		<br />
		Game Info:<br />
		<p class = "last"><label>Date:</label><input id = "info_date" value = "{{ game.date }}" /></p>
		<p class = "last"><label>Orders Due:</label><input id = "info_due_date" value = "{{ game.due_date }}" /></p>
		<p class = "last"><label>Phase:</label>
			<select id = "info_phase">
				{% for phase in phases %}
				<option value = "{% cycle 1,2,3,4 %}" {% ifequal phase current_phase %}selected = "selected"{% endifequal %}>{{ phase }}</option>
				{% endfor %}
			</select>
		</p>
		<p class = "last"><label>&nbsp;</label><input type = "button" value = "Save" onclick = "updateInfo()" /></p>
		<p class = "last"><label>&nbsp;</label><span id = "info_msg"></span></p><br />
	{% endifequal %}
{% else %}
	<a href = "{{ join_url }}">{{ join_text }}</a><br />
{% endifequal %}
<a href = "/games">Back to Games List</a>
{% endblock %}
